home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / PipedWriter.java < prev    next >
Text File  |  1998-09-22  |  3KB  |  116 lines

  1. /*
  2.  * @(#)PipedWriter.java    1.6 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17.  
  18. /**
  19.  * Piped character-output streams.
  20.  *
  21.  * @version     1.6, 98/07/01
  22.  * @author    Mark Reinhold
  23.  * @since    JDK1.1
  24.  */
  25.  
  26. public class PipedWriter extends Writer {
  27.  
  28.     PipedOutputStream byteSource;
  29.  
  30.     private byte buf[];        /* Conversion buffer */
  31.  
  32.     /**
  33.      * Create a writer that is not yet connected to a piped reader.
  34.      */
  35.     public PipedWriter() {
  36.     byteSource = new PipedOutputStream();
  37.     }
  38.  
  39.     /**
  40.      * Create a writer for the specified piped character-input stream.
  41.      */
  42.     public PipedWriter(PipedReader sink) throws IOException {
  43.     this();
  44.     connect(sink);
  45.     }
  46.  
  47.     /** Check to make sure that the stream has not been closed */
  48.     private void ensureOpen() throws IOException {
  49.     if (byteSource == null)
  50.         throw new IOException("Stream closed");
  51.     }
  52.  
  53.     /**
  54.      * Connect the specified piped reader to this writer.
  55.      */
  56.     public void connect(PipedReader sink) throws IOException {
  57.     synchronized (lock) {
  58.         ensureOpen();
  59.         byteSource.connect(sink.byteSink);
  60.     }
  61.     }
  62.  
  63.     /**
  64.      * Write a portion of an array of characters.
  65.      *
  66.      * @param  cbuf  Array of characters
  67.      * @param  off   Offset from which to start writing characters
  68.      * @param  len   Number of characters to write
  69.      *
  70.      * @exception  IOException  If an I/O error occurs
  71.      */
  72.     public void write(char cbuf[], int off, int len) throws IOException {
  73.     synchronized (lock) {
  74.         ensureOpen();
  75.  
  76.         int nb = len * 2;
  77.         if ((buf == null) || (buf.length < nb))
  78.         buf = new byte[nb];
  79.         for (int i = 0; i < nb; i += 2) {
  80.         char c = cbuf[off + (i >> 1)];
  81.         buf[i] = (byte) (c >> 8);
  82.         buf[i + 1] = (byte) c;
  83.         }
  84.  
  85.         byteSource.write(buf, 0, nb);
  86.     }
  87.     }
  88.  
  89.     /**
  90.      * Flush the stream.
  91.      *
  92.      * @exception  IOException  If an I/O error occurs
  93.      */
  94.     public void flush() throws IOException {
  95.     synchronized (lock) {
  96.         ensureOpen();
  97.         byteSource.flush();
  98.     }
  99.     }
  100.  
  101.     /**
  102.      * Close the stream.
  103.      *
  104.      * @exception  IOException  If an I/O error occurs
  105.      */
  106.     public void close() throws IOException {
  107.     synchronized (lock) {
  108.         if (byteSource == null)
  109.         return;
  110.         byteSource.close();
  111.         byteSource = null;
  112.     }
  113.     }
  114.  
  115. }
  116.